import Vue from 'vue'
import Router from 'vue-router'
import store from '../store'
import { Dialog, Notify } from 'vant' // 错误时提示UI

Vue.use(Router)
var router = new Router({
  routes: [
    {
      path: '/',
      name: 'Index',
      component: () => import('@/views/index')
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import('@/views/login')
    },
    {
      path: '/test',
      name: 'Test',
      component: () => import('@/views/test')
    }
  ]
})
const isLogin = store.state.isLogin
// 允许不需要登录也可以展示的页面
const filterRou = ['Login', 'Index']
router.beforeEach((to, from, next) => {
  if (isLogin || filterRou.indexOf(to.name) > -1) {
    next()
  } else {
    if (Vue.prototype.$isWx) {
      Dialog.confirm({
        title: '提示',
        message: '检测到您还没有进行登录，是否前去登录？'
      })
        .then(() => {
          next('/login')
        })
        .catch(() => {
          Notify({ type: 'warning', message: '您取消了登录，可能部分功能会受到限制' })
        })
    }
  }
})
export default router
